import { lStorage } from './cache'
import { refreshToken } from '@/apis/auth';
import { useAccountStore } from '@/stores/account';

const TOKEN_CODE = 'access_token';
const DURATION = 24 * 60 * 60;

export function getToken() {
    return lStorage.get(TOKEN_CODE);
}

export function setToken(token) {
    lStorage.set(TOKEN_CODE, token, DURATION);
}

export function removeToken() {
    lStorage.remove(TOKEN_CODE);
}

export async function refreshAccessToken() {
    const tokenItem = lStorage.getItem(TOKEN_CODE)

    if (!tokenItem)
        return

    const { time } = tokenItem

    // token生成或者刷新后120分钟内不执行刷新
    if (new Date().getTime() - time <= 1000 * 60 * 120) return

    const data = await refreshToken();

    if (data.code === 0)
        setToken(data.content);
    else
        useAccountStore().logout();
}